package com.offcn.scwproject.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.offcn.dycommon.enums.ProjectStatusEnume;
import com.offcn.scwproject.contants.ProjectContants;
import com.offcn.scwproject.enums.ProjectImageTypeEnume;
import com.offcn.scwproject.mapper.*;
import com.offcn.scwproject.pojo.*;
import com.offcn.scwproject.service.ProjectCreateService;
import com.offcn.scwproject.vo.req.ProjectRedisStorageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectCreateServiceImpl implements ProjectCreateService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private TProjectMapper projectMapper;
    @Autowired
    private TProjectImagesMapper projectImagesMapper;
    @Autowired
    private TProjectTagMapper projectTagMapper;
    @Autowired
    private TProjectTypeMapper projectTypeMapper;
    @Autowired
    private TReturnMapper returnMapper;
    @Override
    public String initCreateProject(Integer memberId) {
        String token = UUID.randomUUID().toString().replace("-","");
        //创建一个临时对象
        ProjectRedisStorageVo projectRedisStorageVo = new ProjectRedisStorageVo();
        projectRedisStorageVo.setMemberid(memberId);//设置用户的id
        projectRedisStorageVo.setProjectToken(token);//设置项目令牌
        //把临时对象存入redis
        redisTemplate.opsForValue().set(ProjectContants.TEMP_PROJECT_PREFIX+token, JSON.toJSONString(projectRedisStorageVo));

        return token;
    }

    @Override
    public void saveProjectInfo(ProjectStatusEnume statusEnume, ProjectRedisStorageVo project) {
       //1.保存项目
        TProject tProject = new TProject();
        BeanUtils.copyProperties(project,tProject);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        tProject.setCreatedate(format);//创建时间
        tProject.setDeploydate(format);//设置发布时间
        tProject.setStatus(statusEnume.getCode()+"");//草稿
         projectMapper.insertSelective(tProject);
         //获取项目的id
        Integer projectId = tProject.getId();
        //保存详细图
        List<String> detailsImage = project.getDetailsImage();
        for (String s : detailsImage) {
            TProjectImages tProjectImages = new TProjectImages(null, projectId, s, ProjectImageTypeEnume.DETAILS.getCode());
            projectImagesMapper.insertSelective(tProjectImages);
        }
        //保存头图
        String headerImage = project.getHeaderImage();
        TProjectImages tProjectImages = new TProjectImages(null, projectId, headerImage, ProjectImageTypeEnume.HEADER.getCode());
        projectImagesMapper.insertSelective(tProjectImages);
        //保存标签
        List<Integer> tagids = project.getTagids();
        for (Integer tagid : tagids) {
            TProjectTag tProjectTag = new TProjectTag(null, projectId, tagid);
            projectTagMapper.insertSelective(tProjectTag);
        }
       //保存类型
        List<Integer> typeids = project.getTypeids();
        for (Integer typeid : typeids) {
            TProjectType tProjectType = new TProjectType(null, projectId, typeid);
            projectTypeMapper.insert(tProjectType);

        }
        //保存回报
        List<TReturn> projectReturns = project.getProjectReturns();
        for (TReturn projectReturn : projectReturns) {
            projectReturn.setProjectid(projectId);//回报项目id
            returnMapper.insertSelective(projectReturn);
        }
        //删除redis临时对象
        redisTemplate.delete(ProjectContants.TEMP_PROJECT_PREFIX+project.getProjectToken());


    }
}
